package com.landis.temple.mobile;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity for viewing and adding to a story
 * 
 * @author John Landis 
 * @author Anton Kronaj
 * 
 */
public class TwitterStory extends ListActivity implements OnClickListener 
{
    private static final String TAG = "TwitterStory";
    
    private TwitterStoryDatabase tsdb;
    private Cursor dbResults;
    private SimpleCursorAdapter adapter;
        
    private TextView userView;	// for the username display.
    
    // Filled with twitter user-name on credentials check
    private long myId = 0;
    private long current_story = -1;
    
    //private Twitter twitter;
    
    View add_button;
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.main);
    
        userView = new TextView( this );
        userView = (TextView) findViewById( R.id.username );
        if(TwitterLibrary.userName != null) userView.setText( TwitterLibrary.userName );
        
        
        add_button = findViewById( R.id.create_button );
        add_button.setOnClickListener( this );
        if(!TwitterBackend.isLoggedIn)
        {
        	add_button.setVisibility(View.GONE);
        }
        else
        {
        	add_button.setVisibility(View.VISIBLE);
        }
        
        // display the chapters for a particular story
        tsdb = new TwitterStoryDatabase( this.getApplication().getBaseContext() );
        tsdb = tsdb.open();
        
        current_story = this.getIntent().getExtras().getLong("story_id");
        Log.d("onCreate","story_id is " + current_story );
                
        myId = this.getIntent().getExtras().getLong("author_id");
        
        updateList( );
    }     
    
    
    /*
     * Sets menu options for standard menu button
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        return MyAppMenus.createMenu(menu, this, R.menu.library_menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {       
        return MyAppMenus.menuItemSelected(item, this, myId);
    } 

    /*
     * Toast author and chapter creation date
     * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
     */
    @Override
    protected void onListItemClick( ListView list , View view , int position, long id) 
    {
    	super.onListItemClick(list, view, position, id);
    	Cursor c = dbResults;
    	c.moveToPosition(position);
    	String toastDisplay = "author: " + c.getString(c.getColumnIndex(TwitterStoryDatabase.CHAPTER_AUTHOR_ID))
    			+ "\nDate: "
    			+ c.getString( c.getColumnIndex(TwitterStoryDatabase.CHAPTER_DATE_CREATED));
        Toast.makeText(this, toastDisplay , Toast.LENGTH_SHORT ).show();
    }
    
    /*
     * populate the listview with the chapters of a particular story
     */
    private void updateList( ) {
        if( dbResults != null ) dbResults.close();
        
        dbResults = tsdb.getChapters( current_story );
        startManagingCursor(dbResults);
        
        if( dbResults != null ){
            // xml defined views
            int to[] = new int[] {  R.id.chapter_author   , 
                                    R.id.chapter_text     };
            
            // columns 
            String[] from = { TwitterStoryDatabase.CHAPTER_AUTHOR_ID , 
                              TwitterStoryDatabase.CHAPTER_TEXT      };
            
            adapter = new SimpleCursorAdapter( this              , 
                                               R.layout.chapters , 
                                               dbResults         , 
                                               from              , 
                                               to                );
            setListAdapter(adapter);
        }
        
        getListView().setDivider(null);
        getListView().setDividerHeight(0);
    }

    /*
     * action on button click
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View add_button) 
    {        
        // if library then add story else add chapter
        // Used only in OnActivityResult to determine expected results
        
        Intent intent = new Intent( this , CreateActivity.class );
        
        intent.putExtra( "action"    , TwitterLibrary.CREATE_CHAPTER  );
        intent.putExtra( "author_id" , myId                           );
        intent.putExtra( "story_id"  , current_story                  );
        
        startActivityForResult( intent , TwitterLibrary.CREATE_CHAPTER );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        updateList();
    }
    
    @Override
    protected void onDestroy() 
    {
        Log.d( TAG , "in onDestroy" );
        super.onDestroy();
        if( tsdb != null ) tsdb.close();
    }

    
    
    
    // Activity Life Cycle Functions 
    // Unchanged except for added logging information
    
    @Override
    protected void onPause() 
    {
        Log.d( TAG , "in onPause" );
        super.onPause();
    }

    @Override
    protected void onRestart() 
    {
        Log.d( TAG , "in onRestart" );
        super.onRestart();
    }

    @Override
    protected void onResume() 
    {
        Log.d( TAG , "in onResume" );
        super.onResume();
    }

    @Override
    protected void onStart() 
    {
        Log.d( TAG , "in onStart" );
        super.onStart();
    }

    @Override
    protected void onStop() 
    {
        Log.d( TAG , "in onStop" );
        super.onStop();
    }
}
